package com.xctech.iot.mqtt.sdk.publish;

import com.xctech.iot.mqtt.sdk.config.MqttConfig;
import com.xctech.iot.mqtt.sdk.core.MqttMessageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

/**
 * <pre>
 * mqtt消息 发送
 * </pre>
 *
 * @author: feihu.liang
 * @since: 2024/4/11 14:42
 */
@Component
@RequiredArgsConstructor
public class MqttPublisherService {

    private static final String EVENT_POST_TOPIC = "/sys/{0}/{1}/thing/event/{2}/post";
    private static final String PROPERTY_POST_TOPIC = "/sys/{0}/{1}/thing/event/property/post";

    private final MqttConfig mqttConfig;
    private final MqttPublisher mqttPublisher;

    /**
     * <pre>
     * 发送mqtt消息
     * </pre>
     *
     * @param req
     * @return void
     */
    public void sendMsg(MqttMessageReq req) {
        mqttPublisher.sendMsg(req.getTopic(), req.getQos(), req.getMsg());
    }

    public void sendEvent(String message) {
        // fixme 替换真实的productId deviceId eventId
        String topic = MessageFormat.format(EVENT_POST_TOPIC, mqttConfig.getProductId(), mqttConfig.getDeviceId(), "eventId");
        mqttPublisher.sendMsg(topic, message);
    }

    public void sendProperty(String message) {
        // fixme 替换真实的productId deviceId
        String topic = MessageFormat.format(PROPERTY_POST_TOPIC, mqttConfig.getProductId(), mqttConfig.getDeviceId());
        mqttPublisher.sendMsg(PROPERTY_POST_TOPIC, message);
    }

}
